package gui.productgroup;


import facades.StorageFacade;
import gui.common.*;
import gui.inventory.*;

import coreModel.ProductGroup;
import coreModel.Supply;
import coreModel.Unit;
import gui.product.AddProductController;


/**
 * Controller class for the edit product group view.
 */
public class EditProductGroupController extends Controller 
implements IEditProductGroupController {

	private ProductContainerData target;

	/**
	 * Constructor.
	 * 
	 * @param view Reference to edit product group view
	 * @param target Product group being edited
	 */
	public EditProductGroupController(IView view, ProductContainerData target)
	{
		super(view);

		construct();

		//setting default values
		getView().enableOK(false);
		getView().enableProductGroupName(true);
		getView().enableSupplyUnit(true);
		getView().enableSupplyValue(true);
		this.target=target;
		if(target!=null)
			loadValues();

	}

	//
	// Controller overrides
	//

	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditProductGroupView getView() {
		return (IEditProductGroupView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		boolean enable=true;
		try{
			//checks to see if the supply can be created
			if(getView().getSupplyUnit()==SizeUnits.Count)
			{
				Supply tmpSupply = new Supply(
						Integer.parseInt(getView().getSupplyValue()),Unit.COUNT);
			}
			else
			{
				Supply tmpSupply = new Supply(Float.parseFloat(getView().getSupplyValue()),
						DataConverter.toEnum(getView().getSupplyUnit()));
			}

			if(enable && getView().getProductGroupName().length()>0 &&
					((ProductGroup)target.getTag()).canAddProductGroup(
							getView().getProductGroupName()))
			{
				enable=enable&true;
			}
			else
				enable=false;

		}
		catch(Exception e)
		{

			enable =false;
		}

		getView().enableOK(enable);

	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		if(target==null)
			return;
		getView().enableOK(true);
		try
		{
			getView().setProductGroupName(target.getName());

			// will fail currently because the implementation of productGroup has not been written
			Unit unit=((ProductGroup) target.getTag()).getThreeMonths().getSupplyUnit();
			if(unit==Unit.COUNT)
				getView().setSupplyValue(""+ (int)((ProductGroup)target.getTag()).
						getThreeMonths().getSupplySize());
			else
				getView().setSupplyValue(""+ ((ProductGroup)target.getTag()).
						getThreeMonths().getSupplySize());
			
			getView().setSupplyUnit(DataConverter.toEnum(((ProductGroup)target.getTag()).
					getThreeMonths().getSupplyUnit()));
		}
		catch (Exception e)
		{
			System.err.println("error Loading Product Group");
		}
	}

	//
	// IEditProductGroupController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit product group view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		enableComponents();
	}

	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit product group view.
	 */
	@Override
	public void editProductGroup() 
	{
		ProductGroup tmpProd;
		try
		{
			tmpProd = new ProductGroup(getView().getProductGroupName(),
					((ProductGroup)target.getTag()).getParent(),
					new Supply(Float.parseFloat(getView().getSupplyValue()),
							DataConverter.toEnum(getView().getSupplyUnit())));
			StorageFacade.getInstance().editProductGroup((ProductGroup)target.getTag(),tmpProd);
		} 
		catch (Exception e)
		{
			//it should never get to this part of the code because 
			//it is all checked for above in the enabling
			e.printStackTrace();
		}


	}




}

